/**
 * Execute the user's code.
 * Just a quick and dirty eval.  No checks for infinite loops, etc.
 */
function runJS() {
  var code = Blockly.Generator.workspaceToCode('JavaScript');
  try {
    eval(code);
  } catch (e) {
    alert('Program error:\n' + e);
  }
}

/**
 * Backup code blocks to localStorage.
 */
function backup_blocks() {
  if ('localStorage' in window) {
    var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
    window.localStorage.setItem('arduino', Blockly.Xml.domToText(xml));
  }
}

/**
 * Restore code blocks from localStorage.
 */
function restore_blocks() {
  if ('localStorage' in window && window.localStorage.arduino) {
    var xml = Blockly.Xml.textToDom(window.localStorage.arduino);
    Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
  }
}

/**
* Save Arduino generated code to local file.
*/
function saveCode() {
  var fileName = window.prompt('What would you like to name your file?', 'BlocklyDuino')
  //doesn't save if the user quits the save prompt
  if(fileName){
    var blob = new Blob([Blockly.Arduino.workspaceToCode()], {type: 'text/plain;charset=utf-8'});
    saveAs(blob, fileName + '.ino');
  }
}


/*
* 打成一个压缩包
* @metas_zip是一个object key是文件名  value是文件内容
* @filename是压缩包的名字
* */
function metas_save_zip(metas_zip,filename){

     var zip = new JSZip();
    for(var i in metas_zip){
        zip.file(i,metas_zip[i])
    }

    zip.generateAsync({type:"blob"}).then(function(content) {
    // Force down of the Zip file
    saveAs(content, filename);
    });
}

/**
 * Save blocks to local file.
 * better include Blob and FileSaver for browser compatibility
 */
function save() {
  var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
  var data = Blockly.Xml.domToText(xml);
//  var fileName = window.prompt('是否保存工程文件，文件名?', 'BlocklyDuino');
    var character_data=getSaveConfig(true);
  var metas_zip=new Object();
  metas_zip["blocks.xml"]=data;
    metas_zip["character"]=character_data;
  var zip_filename="MRC_offile.mt";
    metas_save_zip(metas_zip,zip_filename);

//  if(fileName){
//    var blob = new Blob([data], {type: 'text/xml'});
//    saveAs(blob, fileName + ".xml");
//  }
}

/**
 * Load blocks from local file.
 */
function load(event) {
    var block_xml = "";
    var new_zip = new JSZip();
    var project_name=this.files[0]
    new_zip.loadAsync(this.files[0]).then(function (zip) {
        new_zip.file("blocks.xml").async("string")
            .then(function (content) {
                // use content
                block_xml = content;
                if(block_xml!=""){
                     try {
                    var xml = Blockly.Xml.textToDom(block_xml);
                } catch (e) {
                    alert('加载工程文件错误，block文件错误');
                    return;
                }
                var count = Blockly.mainWorkspace.getAllBlocks().length;
                if (count && confirm('Replace existing blocks?\n"Cancel" will merge.')) {
                    Blockly.mainWorkspace.clear();
                }
                Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
                project_name=project_name.name.split(".")[0];
                $("#project_name").text(project_name);
                $("#project_name").css("display","block");
                }

            }
        );
         new_zip.file("character").async("string")
            .then(function (content) {
                initByConfig(content,true);
             });
    });
//  var files = event.target.files;
//  // Only allow uploading one file.
//  if (files.length != 1) {
//    return;
//  }
//
//  // FileReader
//  var reader = new FileReader();
//  reader.onloadend = function(event) {
//    var target = event.target;
//    // 2 == FileReader.DONE
//    if (target.readyState == 2) {
//      try {
//        var xml = Blockly.Xml.textToDom(target.result);
//      } catch (e) {
//        alert('Error parsing XML:\n' + e);
//        return;
//      }
//      var count = Blockly.mainWorkspace.getAllBlocks().length;
//      if (count && confirm('Replace existing blocks?\n"Cancel" will merge.')) {
//        Blockly.mainWorkspace.clear();
//      }
//      Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
//    }
//    // Reset value of input after loading because Chrome will not fire
//    // a 'change' event if the same file is loaded again.
//    document.getElementById('load').value = '';
//  };
//  reader.readAsText(files[0]);
}


/*新建一个空的工程，初始blocks和人物 背景*/
function new_empty_project() {
  var empty_figure_active='{"backgrounds":[],"humans":["./img/human/753c35deaf4b507f.png","./img/human/5f1decb5302f330b.png"],"currentBg":"","currentHm":"./img/human/753c35deaf4b507f.png"}';
  if(confirm('是否清空当前的工程，新建一个空工程？')){
    Blockly.mainWorkspace.clear();
    $("#project_name").text('');
    $("#project_name").css("display","none");
    initByConfig(empty_figure_active,true);
    ActionBlock.isAction();
    ActionBlock.reset();
  }
}

/**
 * Discard all blocks from the workspace.
 */
function discard() {
  var count = Blockly.mainWorkspace.getAllBlocks().length;
    if(confirm("确认清楚已编辑的积木")) {
        if (count < 2 || window.confirm('Delete all ' + count + ' blocks?')) {
            Blockly.mainWorkspace.clear();
            renderContent();
        }
    }
}

/*
 * auto save and restore blocks
 */
function auto_save_and_restore_blocks() {
  // Restore saved blocks in a separate thread so that subsequent
  // initialization is not affected from a failed load.
  window.setTimeout(restore_blocks, 0);
  // Hook a save function onto unload.
  bindEvent(window, 'unload', backup_blocks);
  tabClick(selected);

  // Init load event.
  var loadInput = document.getElementById('load');
  loadInput.addEventListener('change', load, false);
  document.getElementById('fakeload').onclick = function() {
    loadInput.click();
  };
}

/**
 * Bind an event to a function call.
 * @param {!Element} element Element upon which to listen.
 * @param {string} name Event name to listen to (e.g. 'mousedown').
 * @param {!Function} func Function to call when event is triggered.
 *     W3 browsers will call the function with the event object as a parameter,
 *     MSIE will not.
 */
function bindEvent(element, name, func) {
  if (element.addEventListener) {  // W3C
    element.addEventListener(name, func, false);
  } else if (element.attachEvent) {  // IE
    element.attachEvent('on' + name, func);
  }
}

//loading examples via ajax
var ajax;
function createAJAX() {
  if (window.ActiveXObject) { //IE
    try {
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        return null;
      }
    }
  } else if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  } else {
    return null;
  }
}

function onSuccess() {
  if (ajax.readyState == 4) {
    if (ajax.status == 200) {
      try {
      var xml = Blockly.Xml.textToDom(ajax.responseText);
      } catch (e) {
        alert('Error parsing XML:\n' + e);
        return;
      }
      var count = Blockly.mainWorkspace.getAllBlocks().length;
      if (count && confirm('Replace existing blocks?\n"Cancel" will merge.')) {
        Blockly.mainWorkspace.clear();
      }
      Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
    } else {
      alert("Server error");
    }
  }
}

function load_by_url(uri) {
  ajax = createAJAX();
  if (!ajax) {
　　   alert ('Not compatible with XMLHttpRequest');
　　   return 0;
　  }
  if (ajax.overrideMimeType) {
    ajax.overrideMimeType('text/xml');
  }

　　ajax.onreadystatechange = onSuccess;
　　ajax.open ("GET", uri, true);
　　ajax.send ("");
}

function uploadCode(code, callback) {
    var target = document.getElementById('content_arduino');
    var spinner = new Spinner().spin(target);

    var url = "http://"+window.location.host+'/';
    var method = "POST";

    // You REALLY want async = true.
    // Otherwise, it'll block ALL execution waiting for server response.
    var async = true;

    var request = new XMLHttpRequest();

    request.onreadystatechange = function() {
        if (request.readyState != 4) {
            return;
        }

        spinner.stop();

        var status = parseInt(request.status); // HTTP response status, e.g., 200 for "200 OK"
        var errorInfo = null;
        switch (status) {
        case 200:
            break;
        case 0:
            errorInfo = "code 0\n\nCould not connect to server at " + url + ".  Is the local web server running?";
            break;
        case 400:
            errorInfo = "code 400\n\nBuild failed - probably due to invalid source code.  Make sure that there are no missing connections in the blocks.";
            break;
        case 500:
            errorInfo = "code 500\n\nUpload failed.  Is the Arduino connected to USB port?";
            break;
        case 501:
            errorInfo = "code 501\n\nUpload failed.  Is 'ino' installed and in your path?  This only works on Mac OS X and Linux at this time.";
            break;
        default:
            errorInfo = "code " + status + "\n\nUnknown error.";
            break;
        };

        callback(status, errorInfo);
    };

    request.open(method, url, async);
    request.setRequestHeader("Content-Type", "text/plain;charset=utf-8");
    request.send(code);
}

function uploadClick() {
    var code = Blockly.Arduino.workspaceToCode();

    var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
    var xmlText = Blockly.Xml.domToPrettyText(xmlDom);

    var json_data=new Object();
    json_data["arduino_code"]=code;
    json_data["xml"]=xmlText;
    json_data["block_count"]=workspace.getAllBlocks().length;
    json_data["user_edit"]=1;
    var new_code=JSON.stringify(json_data);
    if(confirm("确认烧录代码到Arduino.")) {
        uploadCode(new_code, function (status, errorInfo) {
            if (status == 200) {
                alert("上传成功");
            } else {
                alert("上传失败: " + errorInfo);
            }
        });
    }
}

function resetClick() {
     var code = "#include <U8glib.h>\n" +
       "U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);\n"+
       "static unsigned char  variable76349518[8][32] U8G_PROGMEM={\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},\n" +
       "\n" +
       "};\n" +
       "void  OLED_display_single20106486(int x,int y)\n" +
       "{\n" +
       "u8g.firstPage();\n" +
       "do {\n" +
       "u8g.drawXBMP( x+16*0,y, 16, 16, variable76349518[0]);\n" +
       "u8g.drawXBMP( x+16*1,y, 16, 16, variable76349518[1]);\n" +
       "u8g.drawXBMP( x+16*2,y, 16, 16, variable76349518[2]);\n" +
       "u8g.drawXBMP( x+16*3,y, 16, 16, variable76349518[3]);\n" +
       "u8g.drawXBMP( x+16*4,y, 16, 16, variable76349518[4]);\n" +
       "u8g.drawXBMP( x+16*5,y, 16, 16, variable76349518[5]);\n" +
       "u8g.drawXBMP( x+16*6,y, 16, 16, variable76349518[6]);\n" +
       "u8g.drawXBMP( x+16*7,y, 16, 16, variable76349518[7]);\n" +
       "}\n" +
       "while ( u8g.nextPage() );\n" +
       "}\n" +
       "void setup() {} void loop() {OLED_display_single20106486(0,0);}";
     var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
    var xmlText = Blockly.Xml.domToPrettyText(xmlDom);

    var json_data=new Object();
    json_data["arduino_code"]=code;
    json_data["xml"]=xmlText;
    json_data["block_count"]=workspace.getAllBlocks().length;
    json_data["user_edit"]=0;
    var new_code=JSON.stringify(json_data);

    if(confirm("确认复位Arduino板.")) {
        uploadCode(new_code, function (status, errorInfo) {
            if (status == 200) {
                alert("复位Arduino成功");
            } else {
                alert("复位Arduino失败: " + errorInfo);
            }
        });
    }
}

